<?php
namespace App\Libraries\Decorator\Image;


use App\Libraries\Decorator\DecoratorInterface\ModelDecorator;
use App\Libraries\Tools\URL;
use App\Models\Product\ProductFile;

class ImagesDecorator implements ModelDecorator
{

    protected $product_file;

//    public function __construct(ProductFile $product_file){
//        $this->product_file = $product_file;
//    }

    public function before(){

    }

    public function after(&$data){
        if(!is_array($data)){
            $data = json_decode(json_encode($data), true);
        }
        if (!empty($data['images'])) {
            // 一维数组
            try{
                $images = json_decode($data['images'], true);
                $data['images'] = [];
                if(is_array($images)){
                    foreach($images as $k => $v){
                        $data['images'][$k] = URL::getImage($v);
                    }
                }
            }catch (\Exception $e){
                $data['images'] = [];
            }
        }
        if(is_array($data)){
            foreach($data as &$v){
                $this->after($v);
            }
        }
        return $data;
    }
}